/********************************************************************************
  * @file     SD93F115B_flash.h
  * @author   TEST TEAM 												    	
  * @version  v0															    	
  * @date     Septemper 2022												    	
  * @brief
  *
  * Copyright (C) 2022  Hangzhou SDIC Microelectronics Co., Ltd
**********************************************************************************/


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SD93F115B_FLASH_H
#define __SD93F115B_FLASH_H

#ifdef __cplusplus
 extern "C" {
#endif

/************Includes **********/

#include "SD93F115B.h"  

/**************Exported types ****************/

/************FLASH Status***********/ 
typedef enum
{
	FLASH_BUSY = 1,
	FLASH_ERROR_WRP,
	FLASH_ERROR_PGERR,
	FLASH_COMPLETE,
	FLASH_TIMEOUT
	
}FLASH_Status;

/****************FLASH_Option_Bytes_Write_Protection*********************/
#define FLASH_WRP_Block0               	     ((uint32_t)0x00000001) 
#define FLASH_WRP_Block1              		 ((uint32_t)0x00000002) 
#define FLASH_WRP_Block2             		 ((uint32_t)0x00000004) 
#define FLASH_WRP_Block3             		 ((uint32_t)0x00000008) 
#define FLASH_WRP_Block4             		 ((uint32_t)0x00000010) 
#define FLASH_WRP_Block5            		 ((uint32_t)0x00000020) 
#define FLASH_WRP_Block6            		 ((uint32_t)0x00000040) 
#define FLASH_WRP_Block7            		 ((uint32_t)0x00000080) 
#define FLASH_WRP_Block8            		 ((uint32_t)0x00000100) 
#define FLASH_WRP_Block9            		 ((uint32_t)0x00000200) 
#define FLASH_WRP_Block10           		 ((uint32_t)0x00000400) 
#define FLASH_WRP_Block11            		 ((uint32_t)0x00000800) 
#define FLASH_WRP_Block12             		 ((uint32_t)0x00001000) 
#define FLASH_WRP_Block13            		 ((uint32_t)0x00002000) 
#define FLASH_WRP_Block14              	     ((uint32_t)0x00004000) 
#define FLASH_WRP_Block15              	     ((uint32_t)0x00008000)

#define FLASH_WRP_Block16            		 ((uint32_t)0x00010000) 
#define FLASH_WRP_Block17             		 ((uint32_t)0x00020000) 
#define FLASH_WRP_Block18             		 ((uint32_t)0x00040000) 
#define FLASH_WRP_Block19            		 ((uint32_t)0x00080000) 
#define FLASH_WRP_Block20            		 ((uint32_t)0x00100000) 
#define FLASH_WRP_Block21            		 ((uint32_t)0x00200000) 
#define FLASH_WRP_Block22           		 ((uint32_t)0x00400000) 
#define FLASH_WRP_Block23            		 ((uint32_t)0x00800000) 
#define FLASH_WRP_Block24            		 ((uint32_t)0x01000000) 
#define FLASH_WRP_Block25            		 ((uint32_t)0x02000000) 
#define FLASH_WRP_Block26           		 ((uint32_t)0x04000000) 
#define FLASH_WRP_Block27           		 ((uint32_t)0x08000000) 
#define FLASH_WRP_Block28            		 ((uint32_t)0x10000000) 
#define FLASH_WRP_Block29            		 ((uint32_t)0x20000000)

#define FLASH_WRP_AllBlock              	 ((uint32_t)0x3FFFFFFF)    /*!< Write protection of all Sectors */

#define IS_FLASH_WRP(PAGE) (((PAGE) != 0x0000000))  

/******************  Bit definition for OB_WRP0 register  *********************/
#define  FLASH_OB_nWRP_WRP                   ((uint32_t)0x0000FFFF)

#define  FLASH_OB_WRP0                       ((uint32_t)0x000000FF)       
#define  FLASH_OB_nWRP0                      ((uint32_t)0x0000FF00)       

/******************  Bit definition for OB_WRP1 register  *********************/
#define  FLASH_OB_WRP1                       ((uint32_t)0x00FF0000)     
#define  FLASH_OB_nWRP1                      ((uint32_t)0xFF000000)     

/******************  Bit definition for OB_WRP2 register  *********************/
#define  FLASH_OB_WRP2                       ((uint32_t)0x000000FF)       
#define  FLASH_OB_nWRP2                      ((uint32_t)0x0000FF00)       

/******************  Bit definition for OB_WRP3 register  *********************/
#define  FLASH_OB_WRP3                       ((uint32_t)0x00FF0000)       
#define  FLASH_OB_nWRP3                      ((uint32_t)0xFF000000)       

/********************* FLASH_Timeout_definition *******************/ 
#define FLASH_ER_PRG_TIMEOUT         	     ((uint32_t)0x000B0000)

/*********************** FLASH_Address ***************************/
#define IS_FLASH_Program_Address(Address) (((Address) >= 0x08000000) && ((Address) <= 0x0801DFFF))

/********************** FLASH_OB_DATA_ADDRESS *******************/  
#define IS_OB_Address(Address) (((Address) >= 0x1FFF8200)&&((Address) <= 0x1FFF83FF))
 
#define IS_USER_Data_Address(Address)   (((Address) >= 0x1FFF8400)&&((Address) <= 0x1FFF87FF))

/********************** FLASH_Interrupts ***********************/
#define FLASH_IT_EOP                   FLASH_CR_EOPIE 
#define FLASH_IT_ERR                   FLASH_CR_ERRIE 
#define IS_FLASH_ITSource(IT) (((IT)  == FLASH_CR_EOPIE) || ((IT) == FLASH_CR_ERRIE))

/********************** FLASH_Flags ***************************/ 
#define FLASH_Flag_BSY                 FLASH_SR_BSY     
#define FLASH_Flag_EOP                 FLASH_SR_EOP 
#define FLASH_Flag_PGERR               FLASH_SR_PGERR   
#define FLASH_Flag_WRPRTERR            FLASH_SR_WRPRTERR    
 
#define IS_FLASH_ClearFlag(Flag) (((Flag) == FLASH_Flag_EOP)||\
								  ((Flag) == FLASH_Flag_PGERR)||\
								  ((Flag) == FLASH_Flag_WRPRTERR))

#define IS_FLASH_StatusFlag(Flag) (((Flag) == FLASH_Flag_BSY) ||((Flag) == FLASH_Flag_EOP)||\
								   ((Flag) == FLASH_Flag_PGERR)||((Flag) == FLASH_Flag_WRPRTERR))

/**************** FLASH_Read Protection Level *****************/ 
#define FLASH_OB_RDP_Level_0   				((uint16_t)0x9966)
#define FLASH_OB_RDP_Level_1   				((uint16_t)0xdd22)
//#define FLASH_OB_RDP_Level_2   		    ((uint16_t)0x7788) //RDP字节不可再被编程,一旦进入Level 2，等级无法更改，操作不可逆

#define IS_FLASH_OB_RDP(Level) (((Level) == FLASH_OB_RDP_Level_0)||((Level) == FLASH_OB_RDP_Level_1))//||((Level) == FLASH_OB_RDP_Level_2))

/**************** USER config *****************/
#define OB_User_WDG_SW                      ((uint16_t)0x0001)
#define OB_User_WDG_nSW                     ((uint16_t)0x0000) 

#define IS_OB_User_WDG(WDG_SW)  (((WDG_SW) == OB_User_WDG_SW)||((WDG_SW) == OB_User_WDG_nSW))

#define IS_OB_User_BOR_TML(BOR_TML) (((BOR_TML)>=0x0000)||((BOR_TML)<=0x000f))

#define IS_OB_User_BOR_VTH(BOR_VTH) (((BOR_VTH)>=0x0000)||((BOR_VTH)<=0x0003))

/****************FLASH Memory Programming functions *************************/
void FLASH_Unlock(void);                                                  //FLASH解锁
void FLASH_Lock(void);                                                    //FLASH上锁
FLASH_Status FLASH_ErasePage(uint32_t Page_Address);                      //FLSAH擦除一个Sector,即512个字节
FLASH_Status FLASH_EraseAllPages(void);                                   //整片擦除FLASH,即120K的主闪存        
FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);          //FLSAH字编程
FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);      //FLSAH半字编程
FLASH_Status FLASH_ProgramByte(uint32_t Address, uint8_t Data);           //FLSAH字节编程

/***************FLASH Option Bytes Programming functions *******************/
void FLASH_OB_Unlock(void);                                              //解锁选项字节模块
void FLASH_OB_Lock(void);                                                //锁住选项字节模块
void FLASH_OB_Launch(void);								                 //选项字节数据强制装载,会引起一次系统复位

FLASH_Status FLASH_OB_Erase(void);						                 //擦除选项字节空间，并降为读保护等级0
FLASH_Status FLASH_OB_DEFAULT(void);                                     //将选项字节恢复出厂设置
FLASH_Status FLASH_OB_EnableWRP(uint32_t FLASH_WRP);		             //使能FLASH写保护
FLASH_Status FLASH_OB_RDPConfig(uint16_t FLASH_OB_RDP);		             //配置FLASH读保护等级

FLASH_Status FLASH_OB_User_WDGConfig(uint16_t OB_User_WDG);              //配置看门狗（硬件或软件）
FLASH_Status FLASH_OB_User_BOR_TMLConfig(uint16_t OB_User_BOR_TML);      //配置低压检测信号有效长度
FLASH_Status FLASH_OB_User_BOR_VTHConfig(uint16_t OB_User_BOR_VTH);      //配置低压检测阈值

uint8_t FLASH_OB_GetWDGConfig(void);                                     //获取选项字节用户看门狗配置
uint8_t FLASH_OB_GetBOR_TMLConfig(void);                                 //获取选项字节用户低压检测信号有效长度配置
uint8_t FLASH_OB_GetBOR_VTHConfig(void);                                 //获取选项字节用户低压检测阈值配置
uint8_t FLASH_OB_GetRDP(void);						                     //读取读保护等级状态
uint32_t FLASH_OB_GetWRP(void);							                 //读取写保护状态
FLASH_Status FLASH_OB_GetLoadERRFlagStatus(void);                        //获取选项字节装载错误标志位

/***************FLASH User Data Programming functions *******************/
FLASH_Status FLASH_UserData_EraseDT0ER(void);                                    //擦除用户数据0扇区
FLASH_Status FLASH_UserData_EraseDT1ER(void);                                    //擦除用户数据1扇区
FLASH_Status FLASH_UserData_ProgramWord(uint32_t Address, uint32_t Data);	     //以字的方式对用户数据区进行编程
FLASH_Status FLASH_UserData_ProgramHalfWord(uint32_t Address, uint16_t Data);	 //以半字的方式对用户数据区进行编程
FLASH_Status FLASH_UserData_ProgramByte(uint32_t Address, uint8_t Data);	     //以字节的方式对用户数据区进行编程

/* FLASH Interrupts and flags management functions **********************************/
FLASH_Status FLASH_GetStatus(void);                                     //读FLSAH 忙，写保护错误，编程错误标志位
FLASH_Status FLASH_WaitForLastOperation(uint32_t Timeout);              //等待FLASH操作完成
FLASH_Status FLASH_WaitEOPOperation(uint32_t Timeout);
void FLASH_ITConfig(uint32_t FLASH_IT, FunctionalState NewState);       //FLASH中断配置
FlagStatus FLASH_GetFlagStatus(uint32_t FLASH_Flag);                    //读FLASH标志位状态
void FLASH_ClearFlag(uint32_t FLASH_Flag);                              //清零标志位

void FLASH_OB_PGM( volatile uint16_t * OB_Adress,uint16_t data);
#ifdef __cplusplus
}
#endif

#endif /* __SD93F115B_FLASH_H */


/**********************************END OF FILE**********************************/


